<?php

class Tut_Model extends CI_Model {

    /**
     * Pager default settings
     * @var unknown_type
     */
    protected $_apager = array(
        'base_url' => '',
        'per_page' => 10,
        'uri_segment' => 5,
        'num_links' => 6,
        'total_rows' => null,
        'cur_tag_open' => '<strong id="current_page_product" class="current-page">',
        'cur_tag_close' => '</strong>',
//            'num_tag_open' => '<li class="page">',
//            'num_tag_close' => '</li>',
        'use_page_numbers' => true,
        'next_link' => '»',
        'prev_link' => '«',
//            'prev_tag_open' => '<li>',
//            'prev_tag_close' => '</li>',
//            'first_tag_open' => '<li>',
//            'first_tag_close' => '</li>',
//            'last_tag_open' => '<li>',
//            'last_tag_close' => '</li>',
//            'last_link' => 'Last',
//            'first_link' => 'First',
//            'next_tag_open' => '<li>',
//            'next_tag_close' => '</li>',
//            'previous_tag_open' => '<li>',
//            'previous_tag_close' => '</li>'
    );
    protected $_dropdownView = array(
    );

    /**
     * Total rows number to use with pagination
     * @var int
     */
    protected $_itotal_rows = 0;
    //Property perspective with column in database.
    protected $_aProp;
    public $sdate_format = 'Y-m-d';

    const ACTIVE = 1;
    const INACTIVE = 0;
    const DELETED = 1;
    const UNDELETED = 0;
    const ELE_PER_PAGE_ADMIN = 20;
    const ELE_PER_PAGE_ADMIN_SESNAME = 'ele_per_page';

    public function __construct($properties = null) {
        parent::__construct();
        
        if($properties){
            $this->set($properties);
        }
    }

    public function assign($object = null) {
        if (!$object) {
            return false;
        }

        foreach ($this->_aProp as $prop) {
            $this->$prop = $object->$prop;
        }

        return true;
    }

    public function set($properties) {
        if (is_object($properties)) {
            $properties = (array) $properties;
        }
        
        if (!is_array($properties)) {
            //throw new Exception('The data must be an array or object');
        }
        
        foreach ($properties as $key => $value) {
            $this->$key = $value;
        }
        
        return $this;
    }

    /**
     * Generate paging links.
     *
     * @return html string
     */
    public function get_pager($sbase_url = '', $iuri_segment = 5) {
        if (empty($sbase_url)) {
            show_error('Base url is empty.');
            return false;
        }

        // set params for paging
        $this->load->helper('form');
        $this->load->library('pagination');
        $this->_apager['base_url'] = site_url($sbase_url);
        $this->_apager['uri_segment'] = $iuri_segment;
        $this->_apager['total_rows'] = $this->_itotal_rows;
        $inum_ele_page = $this->session->userdata($this->config->item('ele_per_page'));
        $inum_ele_page = !empty($inum_ele_page) ? $inum_ele_page : $this->config->item('per_page_admin');
        $this->_apager['per_page'] = $inum_ele_page;
        $this->pagination->initialize($this->_apager);

        return $this->pagination->create_links();
    }

    public function set_pager(array $pager = null) {
        if (empty($pager)) {
            return false;
        }

        return $this->_apager = array_merge($this->_apager, $pager);
    }

    public function get_pager_admin($spager = '') {
        $inum_ele_page = $this->session->userdata($this->config->item('ele_per_page'));
        $inum_ele_page = !empty($inum_ele_page) ? $inum_ele_page : $this->config->item('per_page_admin');
        $adropdown_view = array(
            'name' => 'pagination_number_view',
            'option' => array(
                20 => 20,
                30 => 30,
                50 => 50,
                100 => 100,
                200 => 200
            ),
            'selected' => $inum_ele_page,
            'extra' => 'id="pagination_number_view"'
        );
        $this->load->helper('language');
        $spager = !empty($spager) ? $spager : '<b>1</b>';
        $spager = ucfirst(lang("phrase var='general.page'")) . ': ' . $spager . ' ' . lang("phrase var='general.of'") . ' '
                . ceil($this->_itotal_rows / $inum_ele_page) . ' ' . lang("phrase var='general.page'") . '.' . ' ' . ucfirst(lang("phrase var='general.view'")) . ': '
                . form_dropdown($adropdown_view['name'], $adropdown_view['option'], $adropdown_view['selected'], $adropdown_view['extra']) .
                ' ' . lang("phrase var='general.per_page'") . ' | ' . ucfirst(lang("phrase var='general.total'")) . ' ' . $this->_itotal_rows . ' ' . lang("phrase var='general.records'") . '.';

        return $spager;
    }

    public function get_row_max($stable, $scolumn = 'id'){
        if(!$stable){
            return false;
        }
        
        $oresult = $this->db->select("MAX({$scolumn}) as {$scolumn}")->from($stable)->get()->result();
        
        if(!$oresult){
            return false;
        }
        
        return $oresult[0]->$scolumn;    
    }
    
    public function get_max_id($stable){
        if(!$stable){
            return false;
        }
        
        if($id = $this->get_row_max($stable)){
            return $id;
        }
        
        return 0;
    }
    
    public function exist($stable, $awhere){
        if(empty($stable) || empty($awhere)){
            return false;
        }
        
        $oresult = $this->db->select('*')->from($stable)->where($awhere)->get();
        
        if($oresult->num_rows() > 0){
            return true;
        }
        return false;
    }
}

?>
